﻿log("Running Taobao Browser  ...  browseTaobao.js")

main();
function main()
{
    var href = location.href;
    if (href.search("item.htm") != -1)
        browseInit();
    else
        logerr("No Script for this page!");
}
function browseInit()
{
    var documentRenderd = false;
    var scirptsLoaded = false;
    loadScriptAsync("./utilities.js", loadCustomScript);

    function loadCustomScript()
    {
        doWithJQuery(onScirptsLoaded);
    }

    function onScirptsLoaded()
    {
        scirptsLoaded = true;
    }

    tasks.onLoaded = function ()
    {
        documentRenderd = true;
        checkAllLoaded();
    }

    function checkAllLoaded()
    {
        if (documentRenderd && scirptsLoaded)
            browseMain();
        else
            setTimeout(checkAllLoaded, 0);
    }
}

function browseMain()
{
    msg.send("itemTabLoaded");
}
var secondsPerScreen = 0.5;
tasks.goTop = function (arg)
{
    secondsPerScreen = arg.secondsPerScreen;
    //description is a id
    var des = $(description);
    var p = des.offset();
    smoothScrollTo(p.top, function ()
    {
        msg.send("topArrived");
    }, secondsPerScreen);
}

tasks.goMid = function ()
{
    var des = $(description);
    var p = des.offset();
    smoothScrollTo(p.top + des.height() / 2 - window.innerHeight / 2, function ()
    {
        msg.send("midArrived");
    }, secondsPerScreen);
    //msg.send("midArrived");
}

tasks.goBottom = function ()
{
    var des = $(description);
    var p = des.offset();
    smoothScrollTo(p.top + des.height() - window.innerHeight, function ()
    {
        msg.send("bottomArrived");
    }, secondsPerScreen);
    //msg.send("bottomArrived");
}

tasks.goRates = function ()
{
    $("a:contains(累计评论),a:contains(累计评价)")[0].click();
    msg.send("ratesArrived");
}

tasks.goTrades = function ()
{
    $("a:contains(成交记录)")[0].click();
    msg.send("tradesArrived");
}

tasks.goCollect = function ()
{
    var jbutton = $("a:contains(收藏商品), a:contains(收藏宝贝)");
    var p = jbutton.offset();
    smoothScrollTo(p.top - window.innerHeight / 2, function ()
    {
        jbutton[0].click();
        msg.send("collectItemArrived");
    }, secondsPerScreen);
    //msg.send("collectItemArrived");
}

tasks.goCollectShop = function ()
{
    // TODO : progblem : scroll too fast, object not showing!!!!
    var jbutton = $("span:contains(收藏本店), a:contains(收藏店铺)").last();
    var p = jbutton.offset();
    smoothScrollTo(p.top - window.innerHeight / 2, function ()
    {
        jbutton[0].click();
        msg.send("collectShopArrived");
    }, secondsPerScreen);
    //msg.send("collectShopArrived");
}

tasks.updateSubitemsLink = function ()
{
    var button = $("a:contains(查看所有宝贝), a.show-more:not(a.disappear):contains(查看更多宝贝)").last()[0];
    simulateMouseDownOnObject(button);
    msg.send("subItemsLinkUpdated", { link: button.href });
}

tasks.updateShopIndexLink = function ()
{
    var button = document.querySelector(".tb-shop-name a, a.shopLink");
    simulateMouseDownOnObject(button);
    msg.send("shopIndexLinkUpdated", { link: button.href });
}